package com.weipai.education.controller;

import com.weipai.education.model.Customer;
import com.weipai.education.model.ExchangeMessage;
import com.weipai.education.model.Product;
import com.weipai.education.model.ProductPrice;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/11 13:28
 * 
 * 产品控制类
 */
@RestController
@Validated
public class ProductController {
    
    @Autowired
    private ProductService productService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @param key
     * @return
     */
    @RequestMapping(value = "/product/list", method = RequestMethod.GET)
    public Response list(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                         @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                         @RequestParam(value = "key", required = false) Integer key,
                         @RequestParam(value = "productCateId", required = false) String productCateId){
        return productService.list(page, size, key, productCateId);
    }

    /**
     * 公众号首页获取产品
     * @param userId
     * @return
     */
    @RequestMapping(value = "/product/allProduct", method = RequestMethod.GET)
    public Response allProduct(@NotBlank(message = "用户id不能为空") @RequestParam(value = "userId") String userId){
        return productService.allProduct(userId);
    }
    
    /**
     * 产品保存--修改
     * @param product
     * @param desc
     * @return
     */
    @RequestMapping(value = "/product/save", method = RequestMethod.POST)
    public Response save(@Valid Product product, String desc, @NotBlank(message = "产品价格不能为空") String productPrice){
        return productService.save(product, desc, productPrice);
    }

    /**
     * 查询商品详情
     * @param productId
     * @return
     */
    @RequestMapping(value = "/product/getInfoByProductId", method = RequestMethod.GET)
    public Response getInfoByProductId(@NotBlank(message = "产品id不能为空") String productId){
        return productService.getInfoByProductId(productId);
    }

    /**
     * 更改产品的状态
     * @param id
     * @return
     */
    @RequestMapping(value = "/product/changeStatus", method = RequestMethod.GET)
    public Response changeStatus(@NotBlank(message = "产品id不能为空") String id){
        return productService.changeStatus(id);
    }

    /**
     * 产品详情--前端
     * @param productId
     * @return
     */
    @RequestMapping(value = "/product/getProductInfo", method = RequestMethod.GET)
    public Response getProductInfo(@NotBlank(message = "产品id不能为空") String productId,
                                   @NotBlank(message = "用户id不能为空") String userId
                                   ){
        return productService.getProductInfo(productId, userId);
    }

    /**
     * 根据产品id获取全部goodsBar
     * @param productId
     * @param type:如果type为null表示查询此产品下面的所有条形码，如果type==noUse表示查询未使用的条形码
     * @return
     */
    @RequestMapping(value = "/product/getAllGoodsBar", method = RequestMethod.GET)
    public Response getAllGoodsBar(@NotNull(message = "产品id不能为空") Integer productId,
                                   @RequestParam(value = "type", required = false) String type){
        return productService.getAllGoodsBar(productId, type);
    }

    /**
     * 加盟商产品的条形码
     * @param productId
     * @return
     */
    @RequestMapping(value = "/product/getUserGoodsBar", method = RequestMethod.GET)
    public Response getUserGoodsBar(@NotNull(message = "产品id不能为空") Integer productId){
        return productService.getUserGoodsBar(productId);
    }

    /**
     * 根据产品类别id和用户的id得到产品的价格
     * @return
     */
    @RequestMapping(value = "/product/getProductPriceByProductCateIdAndUserId", method = RequestMethod.GET)
    public Response getProductPriceByProductCateIdAndUserId(@NotNull(message = "产品类别id不能为空") Integer productCateId){
        return productService.getProductPriceByProductCateIdAndUserId(productCateId);
    }
    
    /**
     * 根据spuNumber得到产品的基础信息
     * @param spuNumber
     * @return
     */
    @RequestMapping(value = "/product/getProductBySpuNumber", method = RequestMethod.GET)
    public Response getProductBySpuNumber(@NotBlank(message = "产品编号不能为空") String spuNumber){
        return productService.getProductBySpuNumber(spuNumber);
    }
    
    /**
     * 出库添加
     * @param spuNumber
     * @return
     */
    @RequestMapping(value = "/product/exchange", method = RequestMethod.GET)
    public Response exchange(@Valid ExchangeMessage exchangeMsg){
    	return productService.exchange(exchangeMsg);
    }
    
//    public Response exchange(@NotBlank(message = "产品编号不能为空") String spuNumber,@NotBlank(message = "条码或数量") String number,
//    		@NotBlank(message = "是否有退货号") String isExchage,@NotBlank(message = "退货类型") String remark
//    		){
//    	return productService.exchange(spuNumber,number,isExchage,remark);
//    }
    
}
